﻿using System;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Common.Commands.TemplateCommands;
using Common.Views;
using Dto.ApiRequests.TemplateForms;
using Dto.ApiResponses;
using Dto.ApiResponses.TemplateResponses;
using PetaPoco;
using Sanelib.DataView;
using WebApp.Services;

namespace WebApp.ControllerApis
{
    public class TemplateController : SmartApiController
    {
        private readonly IViewRepository<TempleteView> _templeteViewRepository;

        public TemplateController(IUserSession userSession, IMappingEngine mappingEngine, IViewRepository<TempleteView> templeteViewRepository)
            : base(userSession, mappingEngine)
        {
            _templeteViewRepository = templeteViewRepository;
        }

        public HttpResponseMessage Get([FromUri] SearchSpecification form)
        {
            var templeteViewPage = _templeteViewRepository.SearchBySpecification(form, null);
            var response = Mapper.Map<Page<TempleteView>, PageResponse<TemplateLine>>(templeteViewPage);

            return Content(response);
        }

        public HttpResponseMessage Post(AddTemplateForm form)
        {
            var command = Mapper.Map<AddTemplateForm, AddTemplete>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Put(UpdateTemplateForm form)
        {
            var command = Mapper.Map<UpdateTemplateForm, UpdateTemplete>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Delete(Guid id)
        {
            var command = new DeleteTemplete(id);
            return ExecuteCommand(command);
        }
    }
}
